package com.ffox.service.impl;

import com.ffox.entity.CourseFile;
import com.ffox.entity.CourseList;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.filter.JwtAuthenticationFilter;
import com.ffox.mapper.CourseMapper;
import com.ffox.service.CourseService;
import com.ffox.service.dto.CourseDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    FastDfsUtil fastDfsUtil;

/*上传资源*/
    @Override
    public Integer insertCourse(CourseList courseList, MultipartFile[]  files) {
        courseList.setStatus((byte) 0);
        courseList.setCreatedAt(LocalDateTime.now());
//        先存储列表
        courseMapper.insertCourseList(courseList);
//        获取列表对应的id
        Integer listId = courseMapper.getCourseListLastID();
//        将获取的id设置给文件

        for (int i = 0; i < files.length; i++){
            try {
                CourseFile courseFile = new CourseFile();
                byte[] bytes = files[i].getBytes();
                String fileName = files[i].getOriginalFilename();
                String afterLastDot = fileName.substring(fileName.lastIndexOf(".") + 1);
                String url = fastDfsUtil.upload_file(bytes,afterLastDot);
                courseFile.setFilePath(url);
                courseFile.setFileName(fileName);
                courseFile.setFileSize(files[i].getSize());
                courseFile.setListId(listId);
                courseFile.setStatus((byte) 0);
                courseFile.setCreatedAt(LocalDateTime.now());
                courseFile.setFileType(afterLastDot);
                courseMapper.insertCourseFile(courseFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

        return listId;
    }

    /*
    * 获取资源列表*/
    @Override
    public List<CourseList> getCourseList() {
        return courseMapper.getCourseList();
    }
/*根据资源列表id获取对应文件*/
    @Override
    public List<CourseFile> getCourseFile(Integer listId) {
        return courseMapper.getCourseFile(listId);
    }
/*根据userNum获取文件列表*/
    @Override
    public List<CourseList> getCourseListByUserNum(String userNum) {
        return courseMapper.getCourseListByUserNum(userNum);
    }
/*删除资源*/
    @Override
    public Integer deleteCourse(Integer listId) {
        courseMapper.updateCourseList(listId, (byte) 1);
        courseMapper.updateCourseFile(listId, (byte) 1);
        return 1;
    }

    @Override
    public CourseDetails getCourseDetails(Integer listId) {
        CourseList courseList = courseMapper.getCourseListById(listId);
        String avatar = courseMapper.getAvatarByUserNum(courseList.getUserNum());
        CourseDetails courseDetails = new CourseDetails();
        courseDetails.setId(courseList.getId());
        courseDetails.setTitle(courseList.getTitle());
        courseDetails.setContent(courseList.getContent());
        courseDetails.setCreatedAt(courseList.getCreatedAt());
        courseDetails.setUserNum(courseList.getUserNum());
        courseDetails.setAvatar(avatar);
        return courseDetails;
    }
}
